#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
class Solution {
public:
    int shortestSubarray(vector<int>& A, int K) {
        vector<int>sum(A.size() + 1);
        for (int i = 1; i <= A.size(); ++i)
            sum[i] = sum[i - 1] + A[i - 1];
        deque<int>deque;
        int res = INT32_MAX;
        for (int i = 0; i <= A.size(); ++i) {
            while (!deque.empty() && sum[i] <= sum[deque.back()]) 
                deque.pop_back();
            while (!deque.empty() && sum[i] - sum[deque.front()] >= K) {
                res = min(res, i - deque.front());
                deque.pop_front();
            }
            deque.push_back(i);
        }
        return res == INT32_MAX ? -1 : res;
    }
};